import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;

import java.lang.*;
import java.lang.String;
import java.util.HashMap;
import java.util.Map;


/**
 * Created by fjc on 2017/3/14.
 */
public class Hash {
    //直接创建jedis
//    Jedis jedis = new Jedis("localhost",6379);
    //创建对象池
    JedisPool jedisPool = new JedisPool(new JedisPoolConfig(),"localhost",6379);
    //获取jedis
    Jedis jedis = jedisPool.getResource();


    @Test
    public void hSet(){
        //取值
        System.out.println(jedis.hget("user","name"));
        System.out.println(jedis.hgetAll("user"));
    }

    @Test
    public void hDel(){
        // 删除一个或多个哈希表字段
        System.out.println(jedis.hgetAll("user"));
        jedis.hdel("user","sex");
        System.out.println(jedis.hgetAll("user"));
    }

    @Test
    public void hExists(){
        // 查看哈希表 key 中，指定的字段是否存在
        System.out.println(jedis.hexists("user","sex"));
    }

    @Test
    public void hIncrBy(){
        //为哈希表 key 中的指定字段的整数值加上增量
        jedis.hincrBy("user","age",10);
        System.out.println(jedis.hget("user","age"));
    }

    @Test
    public void hIncrByFloat(){
        //为哈希表 key 中的指定字段的浮点数值加上增量
        jedis.hincrByFloat("user","age",0.01);
        System.out.println(jedis.hget("user","age"));
    }

    @Test
    public void hKeys(){
        //获取所有哈希表中的字段
        System.out.println(jedis.hkeys("user"));
    }

    @Test
    public void hLen(){
        //获取哈希表中字段的数量
        System.out.println(jedis.hlen("user"));
    }

    @Test
    public void hmSet(){
        //设置多个字段的值
        Map<String, String> map = new HashMap<java.lang.String, java.lang.String>();
        map.put("name","王尼美");
        map.put("age","24");
        System.out.println(jedis.hmset("user",map));
        System.out.println(jedis.hgetAll("user"));
    }

    @Test
    public void hmGet(){
        //获取多个字段的值
        System.out.println(jedis.hmget("user","name","age"));
    }

    @Test
    public void hSetnx(){
        //只有在字段 field 不存在时，设置哈希表字段的值
        jedis.hsetnx("user","score","99");
        System.out.println(jedis.hgetAll("user"));
    }

    @Test
    public void hVals(){
        // 获取哈希表中所有值
        System.out.println(jedis.hvals("user"));
    }
}
